package com.ly.drivingschool.controller;

import com.ly.drivingschool.common.ResponseMessage;
import com.ly.drivingschool.pojo.*;
import com.ly.drivingschool.service.DictService;
import com.ly.drivingschool.service.StaffService;
import com.ly.drivingschool.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 学员管理
 */
@Slf4j
@RestController
@RequestMapping(value = "user",produces = "application/json; charset=utf-8")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DictService dictService;

    @Autowired
    private StaffService staffService;

    /**
     * 分页查询驾校学员信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getPageUserInfo")
    public ResponseMessage getPageUserInfo(int page, int limit) {
        Pager<User> pager = userService.findByPager(page,limit);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("驾校暂无学员信息",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 模糊查询
     * @param page
     * @param limit
     * @param keyWork
     * @return
     */
    @GetMapping(value = "getKeyWorkUserInfo")
    public ResponseMessage getKeyWorkUserInfo(int page,int limit, String keyWork){
        Pager<User> pager = userService.findByKey(page,limit,keyWork);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("驾校没有该学员信息,请检查",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 选择条件查询
     * @param page
     * @param limit
     * @param col
     * @param code
     * @return
     */
    @GetMapping(value = "findByCondition")
    public ResponseMessage findByCondition(int page, int limit,String col,String code){
        Pager<User> pager = userService.findByCondition(page,limit,col,code);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("无该人员信息,请检查",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 删除一条记录
     * @param user_id
     * @return
     */
    @PostMapping(value = "delByIdUser")
    public ResponseMessage delByIdUser(int user_id) {
        // 删除学员后也删除对应的订单
        int code = userService.deleteByPrimaryKey(user_id);
        return ResponseMessage.createBySuccess(code);
    }

    /**
     * 批量删除
     * @param array
     * @return
     */
    @PostMapping(value = "delByBatchUser")
    public ResponseMessage delByBatchUser(int[] array) {
        int code = userService.deleteByBatchUser(array);
        return ResponseMessage.createBySuccess(code);
    }

    /**
     * 查看单条学员信息
     * @param user_id
     * @return
     */
    @GetMapping(value = "getByIdUser")
    public ResponseMessage getByIdUser(int user_id) {
        User user = userService.selectByPrimaryKey(user_id);
        User userDict = userService.findByDict(user_id);
        System.out.println("学员信息1：" + user);
        System.out.println("学员信息2：" + userDict);

        List<Dict> dictList1 = dictService.getByBelong("xlc001");
        List<Dict> dictList2 = dictService.getByBelong("xlc002");
        List<Dict> dictList3 = dictService.getByBelong("xy001");
        List<Dict> dictList4 = dictService.getByBelong("jk001");

        List<Staff> staff = staffService.getStaffByZG001("021");

        HashMap<String,Object> userHashMap = new HashMap<>();
        userHashMap.put("userCode",user);
        userHashMap.put("userValue",userDict);

        // 字典值
        userHashMap.put("xlc001",dictList1);
        userHashMap.put("xlc002",dictList2);
        userHashMap.put("xy001",dictList3);
        userHashMap.put("jk001",dictList4);

        userHashMap.put("staff",staff);



        if (user == null) {
            return ResponseMessage.createByErrorMessage("查无此人");
        } else {
            return ResponseMessage.createBySuccess("查看学员信息",userHashMap);
        }
    }

    /**
     * 更新记录
     * @param user
     * @return
     */
    @PostMapping(value = "updateByIdUser")
    public ResponseMessage updateByIdUser(@RequestBody User user){
        System.out.println("获取的user信息：" + user.toString());
        int code = userService.updateByPrimaryKeySelective(user);
        if (code == 0) {
            return ResponseMessage.createByErrorMessage("更新失败");
        } else {
            return ResponseMessage.createBySuccess("更新成功", code);
        }
    }

    /**
     * 添加新学员信息
     * @param user
     * @return
     */
    @PostMapping(value = "addUserInfo")
    public ResponseMessage addUserInfo(@RequestBody User user){
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        String createTime = df.format(calendar.getTime());
        // 封装创建时间
        user.setCreateTime(createTime);
        // 封装默认普通学员 xy001 = 008 ---- 普通学员
        user.setXy001("008");
        System.out.println(user.toString());
        int code = userService.insertSelective(user);
        return ResponseMessage.createBySuccess(code);
    }
}
